//
// Created by shaowenhan on 2020/5/9.
//

#include "ConfigurableEventListener.h"
#include "ReadInstanceWrapper.h"
class TestReadInstanceWrapper: public testing::Test{
public:
    ReadInstanceWrapper readInstanceWrapper;
    TestReadInstanceWrapper():readInstanceWrapper("../../Test/instance"){}
};
TEST_F(TestReadInstanceWrapper, SplitLineContainIntegersWithOneSeparatorBetElements)
{
    string line("2 2 3 2");
    vector<string> expectedResult({"2", "2", "3", "2"});
    int elementCount;

    auto elements = readInstanceWrapper.elementsInLine(line, elementCount, vector<char>({' '}));

    ASSERT_EQ(expectedResult.size(), elementCount);
    for(int i = 0; i < elementCount; i++)
        ASSERT_EQ(expectedResult[i], elements[i]);
}

TEST_F(TestReadInstanceWrapper, SplitLineContainIntegersWithVariousSeparatorsBetElements)
{
    string line("1 37 52 0   7 1 4 1 2 4 8");
    vector<string> expectedResult({"1", "37", "52", "0", "7", "1", "4", "1", "2", "4", "8"});
    int elementCount;

    auto elements = readInstanceWrapper.elementsInLine(line, elementCount, vector<char>({' '}));

    ASSERT_EQ(expectedResult.size(), elementCount);
    for(int i = 0; i < elementCount; i++)
        ASSERT_EQ(expectedResult[i], elements[i]);
}

TEST_F(TestReadInstanceWrapper, SplitLineContainIntegersAndTransformThemIntoIntegers)
{
    string line("2 2 13 2");
    vector<int> expectedResult({2, 2, 13, 2});
    int elementCount;

    auto elements = readInstanceWrapper.integersInLine(line, elementCount, vector<char>({' '}));

    ASSERT_EQ(expectedResult.size(), elementCount);
    for(int i = 0; i < elementCount; i++)
        ASSERT_EQ(expectedResult[i], elements[i]);
}
TEST_F(TestReadInstanceWrapper, SplitLineContainDoublesAndTransformThemIntoDoubles)
{
    string line("1 -29.730  64.136  2 12 1 4 1 2 4 8");
    vector<double> expectedResult({1, -29.730, 64.136, 2, 12, 1, 4, 1, 2, 4, 8});
    int elementCount;

    auto elements = readInstanceWrapper.doublesInLine(line, elementCount, vector<char>({' '}));

    ASSERT_EQ(expectedResult.size(), elementCount);
    for(int i = 0; i < elementCount; i++)
        ASSERT_FLOAT_EQ(expectedResult[i], elements[i]);
}

TEST_F(TestReadInstanceWrapper, IsEndOfFile)
{
    ASSERT_FALSE(readInstanceWrapper.isEOF());
}
TEST_F(TestReadInstanceWrapper, ReadFirstLine)
{
    string expectedLine("2 2 3 2");

    string line = readInstanceWrapper.readLine();

    ASSERT_EQ(expectedLine, line);
}
TEST_F(TestReadInstanceWrapper, ReadTheWholeFile)
{
    vector<string> expectedRes({
        "2 2 3 2",
        "0 80",
        "0 80",
        "1 37 52 0   7 1 4 1 2 4 8",
        ""
    });

    int lineNum = 0;
    while(not readInstanceWrapper.isEOF())
    {
        string line = readInstanceWrapper.readLine();
        ASSERT_EQ(expectedRes[lineNum++], line);
    }
    ASSERT_EQ(expectedRes.size(), lineNum);
}
GTEST_API_ int main(int argc, char** argv) {
    testing::InitGoogleTest(&argc, argv);
    initialGTest();
    return RUN_ALL_TESTS();
}